package dk.mirasola.systemtraining.bridgewidgets.shared.model.distributionfiltertree.shape;

import com.google.gwt.user.client.rpc.IsSerializable;
import dk.mirasola.systemtraining.bridgewidgets.shared.model.Hand;
import dk.mirasola.systemtraining.bridgewidgets.shared.model.Suit;

import java.util.ArrayList;
import java.util.List;

public class Shape implements IsSerializable {
    public static final Shape SHAPE_4333 = new Shape(ExactShapeElement.create(4, false), ExactShapeElement.create(3, false),
            ExactShapeElement.create(3, false), ExactShapeElement.create(3, false));
    public static final Shape SHAPE_4432 = new Shape(ExactShapeElement.create(4, false), ExactShapeElement.create(4, false),
            ExactShapeElement.create(3, false), ExactShapeElement.create(2, false));

    public static boolean validate(List<ShapeElement> shapeElements) {
        checkParmeter(shapeElements);

        int min = 0;
        int max = 0;

        for (ShapeElement shapeElement : shapeElements) {
            if (shapeElement instanceof WildcardShapeElement) {
                max += 13;
            } else if (shapeElement instanceof ExactShapeElement) {
                ExactShapeElement exact = (ExactShapeElement) shapeElement;
                min += exact.getCardsCount();
                max += exact.getCardsCount();
            } else if (shapeElement instanceof IntervalShapeElement) {
                IntervalShapeElement interval = (IntervalShapeElement) shapeElement;
                min += interval.getMinCardsCount();
                max += interval.getMaxCardsCount();
            } else {
                throw new RuntimeException("Unknown shape element: " + shapeElement);
            }
        }

        return min <= 13 && max >= 13;
    }

    private static void checkParmeter(List<ShapeElement> shapeElements) {
        if (shapeElements == null) {
            throw new NullPointerException();
        }

        if (shapeElements.size() != 4) {
            throw new IllegalArgumentException("list of shape elements must contain exactly 4");
        }
    }

    public static Shape create(List<ShapeElement> shapeElements, boolean validate) {
        if (validate && !validate(shapeElements)) {
            throw new InvalidShapeException();
        } else {
            checkParmeter(shapeElements);
        }
        return new Shape(shapeElements);
    }

    private List<ShapeElement> shapeElements;

    Shape() {
    }

    private Shape(ShapeElement spades, ShapeElement hearts, ShapeElement diamonds, ShapeElement clubs) {
        this.shapeElements = new ArrayList<ShapeElement>();
        shapeElements.add(spades);
        shapeElements.add(hearts);
        shapeElements.add(diamonds);
        shapeElements.add(clubs);
    }

    private Shape(List<ShapeElement> shapeElements) {
        this.shapeElements = new ArrayList<ShapeElement>(shapeElements);
    }

    public ShapeElement getShapeElement(Suit suit) {
        if (suit == null) {
            throw new NullPointerException();
        }

        switch (suit) {
            case SPADE:
                return shapeElements.get(0);
            case HEART:
                return shapeElements.get(1);
            case DIAMOND:
                return shapeElements.get(2);
            case CLUB:
                return shapeElements.get(3);
            default:
                throw new RuntimeException("unknown suit:" + suit);
        }
    }


    public List<ShapeElement> getShapeElements() {
        return new ArrayList<ShapeElement>(shapeElements);
    }


    public String toString() {
        StringBuilder builder = new StringBuilder();
        for (ShapeElement shapeElement : shapeElements) {
            builder.append(shapeElement);
            builder.append("-");
        }
        builder.deleteCharAt(builder.length() - 1);
        return builder.toString();
    }

    public boolean match(Hand hand) {
        for (int i = 0; i < 4; i++) {
            ShapeElement shapeElement = shapeElements.get(i);
            int cardsCount = -1;
            switch (i) {
                case 0:
                    cardsCount = hand.getCardsCount(Suit.SPADE);
                    break;
                case 1:
                    cardsCount = hand.getCardsCount(Suit.HEART);
                    break;
                case 2:
                    cardsCount = hand.getCardsCount(Suit.DIAMOND);
                    break;
                case 3:
                    cardsCount = hand.getCardsCount(Suit.CLUB);
                    break;
            }

            if (!shapeElement.match(cardsCount)) {
                return false;
            }
        }
        return true;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Shape shape = (Shape) o;
        return shapeElements.equals(shape.shapeElements);
    }

    @Override
    public int hashCode() {
        return shapeElements.hashCode();
    }
}
